using UnityEngine;
using System.Collections;

public class InverterGravity : MonoBehaviour
{
	private GameObject player;
	
	void Start ()
	{
		player = GameObject.FindGameObjectWithTag (Tags.player);
	}
	
	void OnTriggerEnter (Collider collider)
	{
		if (collider.transform.tag.Equals(Tags.player)) {
			//Rodar o personagem para que ele possa encostar em novos pisos.
			changeGravity ();
		}
	}
	
	private void changeGravity ()
	{
		Player_Movement pm = player.GetComponent<Player_Movement> ();
		
		//Altera a gravidade do jogador e também zera sua velocidade para não ocorrer parábola
		switch (pm.EnumGravity) {
		case Enum_Gravity.UP:
			pm.setGravity (Vector3.zero);
            Physics.gravity = new Vector3(-9.8f, 0f, 0f);
            player.transform.rotation = Quaternion.Euler(0, 0, 270f);
			pm.EnumGravity = Enum_Gravity.LEFT;
			break;
			
		case Enum_Gravity.RIGHT:
			pm.setGravity (Vector3.zero);
            Physics.gravity = new Vector3(0f, 9.8f, 0f);
			player.transform.rotation = Quaternion.Euler(0, 0, 180f);
			pm.EnumGravity = Enum_Gravity.UP;
			break;
			
		case Enum_Gravity.DOWN:
			pm.setGravity (Vector3.zero);
            Physics.gravity = new Vector3(9.8f, 0f, 0f);
			player.transform.rotation = Quaternion.Euler(0, 0, 90f);
			pm.EnumGravity = Enum_Gravity.RIGHT;
			break;
			
		case Enum_Gravity.LEFT:
			pm.setGravity (Vector3.zero);
            Physics.gravity = new Vector3(0f, -9.8f, 0f);
			player.transform.rotation = Quaternion.Euler(0, 0, 0f);
			pm.EnumGravity = Enum_Gravity.DOWN;
			break;
			
		default:
			Debug.LogError ("InverterGravity - Gravidade Inexistente!");
			break;
		}
	}
	
	void Update()
	{
		transform.Rotate(0,0,-2);
	}
}
